package launch;

import java.awt.BorderLayout;
import java.awt.Choice;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.IOException;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.text.JTextComponent;

import modeles.DES_Encrypt;
import modeles.TripleDES;

/**
 * Classe représentant l'interface graphique de notre application.
 * @author ejouini
 *
 */
public class Launcher extends JFrame implements ActionListener {

	/** Panel contenant tous les composants de notre fenêtre. */
	public JPanel panelPrincipal;

	/** JTextField qui contiendra le texte à crypter. */
	public JTextArea texteAEncrypter;

	/** JTextField qui contiendra le texte à decrypter. */
	public JTextArea texteADecrypter;

	/** Champ texte qui contiendra le clé pour DES. */
	public JTextField desKey;

	/** Trois champs texte qui contiendront les clés pour triple DES. */
	public JTextField tripleDesKey1, tripleDesKey2, tripleDesKey3;

	/**	Radio bouton de selection du type de chriffrement (ici DES). */
	public JRadioButton btnChoixDes;

	/** Radio bouton de selection du type de chiffrement (ici TripleDES).*/
	public JRadioButton btnChoixTripleDes;

	/** Groupe des boutons radio pour qu'un seul soit sélectionnable */
	public ButtonGroup groupeRadioBtn;

	/** Listbox permettant de choisir l'encryption mode. */
	public Choice encryptionMode;

	/** Bouton qui permettra de lancer l'encryption */
	public JButton btnEncrypter;

	/** Bouton qui permettra de lancer l'encryption */
	public JButton btnDecrypter;

	public JLabel lblTexteEncrypter;
	public JLabel lblTexteDecrypter;
	public JLabel lblChiffrement;
	public JLabel lblEncryptionMode;
	public JLabel lblKey;




	public Launcher () {
		panelPrincipal = new JPanel(new GridLayout(8, 1));


		btnEncrypter = new JButton("Encrypter");
		btnDecrypter = new JButton("Decrypter");

		texteAEncrypter = new JTextArea();

		texteAEncrypter.setLineWrap(true);
		texteADecrypter = new JTextArea();
		texteADecrypter.setLineWrap(true);
		texteADecrypter.setEditable(false);
		desKey = new JTextField();
		desKey.setPreferredSize(new Dimension(70, 25));
		tripleDesKey1 = new JTextField();
		tripleDesKey1.setPreferredSize(new Dimension(70, 25));
		tripleDesKey2 = new JTextField();
		tripleDesKey2.setPreferredSize(new Dimension(70, 25));
		tripleDesKey3 = new JTextField();
		tripleDesKey3.setPreferredSize(new Dimension(70, 25));

		btnChoixDes = new JRadioButton("Chiffrement DES ");
		btnChoixDes.setSelected(true);
		btnChoixTripleDes = new JRadioButton("Chiffrement Triple DES ");


		groupeRadioBtn = new ButtonGroup();
		lblTexteEncrypter = new JLabel("Plain text :");
		lblTexteDecrypter = new JLabel("Cipher text :");
		lblChiffrement = new JLabel("Choisissez un chiffrement");
		lblKey = new JLabel("Key");
		lblEncryptionMode = new JLabel("Encryption mode");

		JPanel panelLblEncryption = new JPanel();
		JPanel panelTxtEncryption = new JPanel(new BorderLayout());
		JPanel panelDES = new JPanel(new BorderLayout());
		JPanel panelTripleDES = new JPanel(new BorderLayout());
		JPanel panelEncryption = new JPanel(new BorderLayout());
		JPanel panelLblDecryption = new JPanel();
		JPanel panelTxtDecryption = new JPanel(new BorderLayout());
		JPanel panelBtn = new JPanel();


		encryptionMode = new Choice();
		encryptionMode.add("ECB");
		encryptionMode.add("CBC");
		encryptionMode.add("CFB");
		encryptionMode.add("OFB");

		groupeRadioBtn.add(btnChoixDes);
		groupeRadioBtn.add(btnChoixTripleDes);

		panelLblEncryption.add(lblTexteEncrypter);
		panelTxtEncryption.add(texteAEncrypter);

		JPanel ssPanel = new JPanel();
		ssPanel.add(btnChoixDes);
		ssPanel.add(desKey);
		panelDES.add(ssPanel, BorderLayout.WEST);

		JPanel ssPanel2 = new JPanel();
		ssPanel2.add(btnChoixTripleDes);
		ssPanel2.add(tripleDesKey1);
		ssPanel2.add(tripleDesKey2);
		ssPanel2.add(tripleDesKey3);
		panelTripleDES.add(ssPanel2, BorderLayout.WEST);

		panelLblDecryption.add(lblTexteDecrypter);
		panelTxtDecryption.add(texteADecrypter);

		JPanel ssPanel3 = new JPanel();
		ssPanel3.add(lblEncryptionMode);
		ssPanel3.add(encryptionMode);
		panelEncryption.add(ssPanel3, BorderLayout.WEST);

		panelBtn.add(btnEncrypter);
		panelBtn.add(btnDecrypter);

		panelPrincipal.add(panelLblEncryption);
		panelPrincipal.add(panelTxtEncryption);
		panelPrincipal.add(panelLblDecryption);
		panelPrincipal.add(panelTxtDecryption);
		panelPrincipal.add(panelDES);
		panelPrincipal.add(panelTripleDES);
		panelPrincipal.add(panelEncryption);
		panelPrincipal.add(panelBtn);

		this.add(panelPrincipal);
		//this.setJMenuBar(menuBar);

		//action controler
		btnChoixDes.addActionListener(this);
		btnChoixTripleDes.addActionListener(this);
		btnEncrypter.addActionListener(this);
		btnDecrypter.addActionListener(this);		


		//*** Mise en forme de la fenetre principale ***//
		setSize(new Dimension(520, 370));
		this.setTitle("Projet Sécurité : Algorithme DES");
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setVisible (true);
		this.setLocation(500, 200);

		desKey.setText("Key");
		desKey.setForeground(Color.GRAY);
		tripleDesKey1.setText("Key1");
		tripleDesKey1.setForeground(Color.GRAY);
		tripleDesKey2.setText("Key2");
		tripleDesKey2.setForeground(Color.GRAY);
		tripleDesKey3.setText("Key3");
		tripleDesKey3.setForeground(Color.GRAY);


		// Evenements
		desKey.addMouseListener(new Mouse(){
			@Override
			public void mouseClicked(MouseEvent arg0) {
				if (desKey.getText().equals("Key")){
					desKey.setText("");
					desKey.setForeground(Color.BLACK);
				}
			}
		});

		tripleDesKey1.addMouseListener(new Mouse(){
			@Override
			public void mouseClicked(MouseEvent arg0) {
				if (tripleDesKey1.getText().equals("Key1")){
					tripleDesKey1.setText("");
					tripleDesKey1.setForeground(Color.BLACK);
				}
			}
		});

		tripleDesKey2.addMouseListener(new Mouse(){
			@Override
			public void mouseClicked(MouseEvent arg0) {
				if (tripleDesKey2.getText().equals("Key2")){
					tripleDesKey2.setText("");
					tripleDesKey2.setForeground(Color.BLACK);
				}
			}
		});

		tripleDesKey3.addMouseListener(new Mouse(){
			@Override
			public void mouseClicked(MouseEvent arg0) {
				if (tripleDesKey3.getText().equals("Key3")){
					tripleDesKey3.setText("");
					tripleDesKey3.setForeground(Color.BLACK);
				}
			}
		});

	}



	@Override
	public void actionPerformed(ActionEvent e) {
		String message = texteAEncrypter.getText();
		int[] bloc_crypt = null;

		if(e.getActionCommand().equals("Encrypter")) {
			
			if (btnChoixDes.isSelected()){
				System.out.println("DES encryption");
				String key = desKey.getText();
				boolean keyok = DES_Encrypt.verifKey(key);
				if (!keyok){
					JOptionPane.showMessageDialog(this, "Veuillez vérifier que votre clé fait 8 bytes", "Erreur taille clé", JOptionPane.ERROR_MESSAGE);
				}
				//ok on encrypt
				else {
					DES_Encrypt des = new DES_Encrypt(key, message);
					bloc_crypt = des.doEncryption(encryptionMode.getSelectedItem());
					String crypt_bloc = "";
					for (int i =0; i<bloc_crypt.length; i++){
						crypt_bloc+=String.valueOf(bloc_crypt[i]);
					}
					texteADecrypter.setText(DES_Encrypt.retour_decrypt);
				}

			}

			//Triple DES
			else {
				System.out.println("Triple DES encryption");
				String key1 = tripleDesKey1.getText();
				String key2 = tripleDesKey2.getText();
				String key3 = tripleDesKey3.getText();
				if (key2.equals("Key2"))
					key2 = key1;
				if (key3.equals("Key3"))
					key3 = key1;
				if (!DES_Encrypt.verifKey(key1)){
					JOptionPane.showMessageDialog(this, "Veuillez vérifier que la clé 1 fait 8 bytes", "Erreur taille clé", JOptionPane.ERROR_MESSAGE);
				}
				else if (!DES_Encrypt.verifKey(key2)){
					JOptionPane.showMessageDialog(this, "Veuillez vérifier que la clé 2 fait 8 bytes", "Erreur taille clé", JOptionPane.ERROR_MESSAGE);
				}	
				else if (!DES_Encrypt.verifKey(key3)){
					JOptionPane.showMessageDialog(this, "Veuillez vérifier que la clé 3 fait 8 bytes", "Erreur taille clé", JOptionPane.ERROR_MESSAGE);
				}
				//ok on encrypt
				else {
					TripleDES tdes = new TripleDES(key1, key2, key3, message);
					bloc_crypt = tdes.doEncryption(encryptionMode.getSelectedItem());
					String crypt_bloc = "";
					for (int i =0; i<bloc_crypt.length; i++){
						crypt_bloc+=String.valueOf(bloc_crypt[i]);
					}
					texteADecrypter.setText(DES_Encrypt.retour_decrypt);
				}


			}


		}
		else if (e.getActionCommand().equals("Decrypter")){
			if (btnChoixDes.isSelected()){
				System.out.println("DES decryption");
				String key = desKey.getText();
				boolean keyok = DES_Encrypt.verifKey(key);
				if (!keyok){
					JOptionPane.showMessageDialog(this, "Veuillez vérifier que votre clé fait 8 bytes", "Erreur taille clé", JOptionPane.ERROR_MESSAGE);
				}
				//ok on encrypt
				else {
					DES_Encrypt des = new DES_Encrypt(key, message);
					bloc_crypt = des.doEncryption(encryptionMode.getSelectedItem());
					des.doDecryption(bloc_crypt, encryptionMode.getSelectedItem());					
					texteADecrypter.setText(DES_Encrypt.retour_decrypt);
				}

			}

			//Triple DES
			else {
				System.out.println("Triple DES decryption");
				String key1 = tripleDesKey1.getText();
				String key2 = tripleDesKey2.getText();
				String key3 = tripleDesKey3.getText();
				if (key2.equals("Key2"))
					key2 = key1;
				if (key3.equals("Key3"))
					key3 = key1;
				if (!DES_Encrypt.verifKey(key1)){
					JOptionPane.showMessageDialog(this, "Veuillez vérifier que la clé 1 fait 8 bytes", "Erreur taille clé", JOptionPane.ERROR_MESSAGE);
				}
				else if (!DES_Encrypt.verifKey(key2)){
					JOptionPane.showMessageDialog(this, "Veuillez vérifier que la clé 2 fait 8 bytes", "Erreur taille clé", JOptionPane.ERROR_MESSAGE);
				}	
				else if (!DES_Encrypt.verifKey(key3)){
					JOptionPane.showMessageDialog(this, "Veuillez vérifier que la clé 3 fait 8 bytes", "Erreur taille clé", JOptionPane.ERROR_MESSAGE);
				}
				//ok on encrypt
				else {
					TripleDES tdes = new TripleDES(key1, key2, key3, message);
					bloc_crypt = tdes.doEncryption(encryptionMode.getSelectedItem());
					tdes.doDecryption(bloc_crypt, encryptionMode.getSelectedItem());
					texteADecrypter.setText(DES_Encrypt.retour_decrypt);
				}


			}


		}

	}


	public static void main(String[] args) {

		Launcher l = new Launcher();


	}


}
